/**
 * 系统平台
 * moonvip_admin
 * com.tz.web.shopbanner
 * ShopBannerController.java
 * 创建人:小胡
 * 时间：2017年02月06日 15:49:44
 *
 */
package com.tz.web.shopbanner;

import com.tz.auth.AuthPassport;
import com.tz.bean.ShopBanner;
import com.tz.bean.TzParams;
import com.tz.service.shopbanner.IShopBannerService;
import com.tz.util.StringHelper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;

/**
 * 商城海报管理web
 * ShopBannerController
 * 创建人:小胡
 * 时间：2017年02月06日 15:49:44
 * @version 1.0.0
 * 
 */
@Controller
@RequestMapping("/admin/shop/shopbanner")
public class ShopBannerController {
	
	@Resource
	private IShopBannerService shopbannerService;

	private final  static  String key_suff =  "shopbanner";


	/*列表查询*/
	@AuthPassport
	@RequestMapping("/list")
	public String list(TzParams params){
		return "shop/shopbanner/list";
	}

    /*模板页面*/
	@RequestMapping("/template")
	public String template(TzParams params ,Model model){
        params.setDeleteCode("001");
		model.addAttribute("datas", shopbannerService.findShopBanners(params));
		model.addAttribute("itemCount", shopbannerService.count(params));
		return "shop/shopbanner/template";
	}



	@AuthPassport
	@RequestMapping("/add")
    public String add(TzParams params){
		return "shop/shopbanner/add";
   }

	@AuthPassport
	@RequestMapping(value = "/edit/{id}" ,method = RequestMethod.GET)
    public String edit(@PathVariable("id") Integer id , Model model){
		HashMap<String, Object> shopbanner = shopbannerService.getShopBanner(id);
		model.addAttribute("shopbanner",shopbanner);
		return "shop/shopbanner/edit";
	}

	@AuthPassport
	@ResponseBody
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	public String delete(TzParams params){
		shopbannerService.delete(params);
		return "success";
	}


	@ResponseBody
	@RequestMapping(value="/update",method= RequestMethod.POST)
	public String update(ShopBanner shopbanner){
		shopbanner.setUpdateTime(new Date());
		shopbannerService.update(shopbanner);
		return "success";
	}

	@RequestMapping(value="/updateShopBanner",method= RequestMethod.POST)
	public String updateShopBanner(ShopBanner shopbanner) throws IOException {
		if(shopbanner.getImgurl()!=null && shopbanner.getImgurl().indexOf("http://")==-1){
			String imgurl = StringHelper.uploadAndgetUrl(key_suff , shopbanner.getImgurl());
			shopbanner.setImgurl(imgurl);
		}
		shopbanner.setUpdateTime(new Date());
		shopbannerService.update(shopbanner);
		return "redirect:list";
	}


	@RequestMapping(value="/save",method= RequestMethod.POST)
	public String save(ShopBanner shopbanner) throws IOException {
		//获得七牛子商品图片外链
		String imgurl = StringHelper.uploadAndgetUrl(key_suff , shopbanner.getImgurl());
		shopbanner.setImgurl(imgurl);
		shopbannerService.save(shopbanner);
		return "redirect:list";
	}

}
